/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomerPnl.java
 *
 * Created on Jul 17, 2011, 11:40:48 AM
 */
package com.viweb.storemanager.ui.customer;

import com.viweb.storemanager.core.ColumnData;
import com.viweb.storemanager.core.CursorManager;
import com.viweb.storemanager.core.DefaultObjectTableModel;
import com.viweb.storemanager.core.OptionPane;
import com.viweb.storemanager.dao.Biz;
import com.viweb.storemanager.entity.Customer;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

/**
 *
 * @author qhvic
 */
public class CustomerPnl extends javax.swing.JPanel {

    private JTable headerTable;
    private ArrayList<Customer> customers;
    private CustomerAddEditPnl addEditPanel;

    /** Creates new form CustomerPnl */
    public CustomerPnl() {
        this.customers = new ArrayList<Customer>();
        initCustomersList();
        initComponents();
        addEditPanel = new CustomerAddEditPnl(this);
    }

    private void initCustomersList() {
        this.customers.clear();
        this.customers = Biz.getInstance().selectAllCustomer();
    }

    public CustomerAddEditPnl getCreationPanel() {
        return this.addEditPanel;
    }

    private void tblListSelectionValueChanged(javax.swing.event.ListSelectionEvent evt) {
        if (!evt.getValueIsAdjusting()) {
            if (this.tblList.getSelectedRow() >= 0) {
                Customer customer = this.customers.get(this.tblList.getSelectedRow());
                this.addEditPanel.assignDrink(customer);
                this.addEditPanel.setEditableMode(false);
            }
        }
    }

    public void updateComponentsUI(Customer customer) {
        this.initCustomersList();
        int rowSelection = this.customers.indexOf(customer);
        this.tblList.getSelectionModel().setSelectionInterval(rowSelection, rowSelection);
        this.headerTable.updateUI();
        this.tblList.updateUI();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        scpList = new javax.swing.JScrollPane();
        tblList = new javax.swing.JTable();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        scpList.setPreferredSize(new java.awt.Dimension(452, 402));

        tblList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tblList.setRowHeight(22);
        ColumnData[] tableColumns = {
            new ColumnData("Mã khách hàng", 100, SwingConstants.LEFT, 1),
            new ColumnData("Họ tên", 100, SwingConstants.LEFT, 2),
            new ColumnData("Loại khách hàng", 100, SwingConstants.LEFT, 3),
            new ColumnData("Địa chỉ", 100, SwingConstants.LEFT, 4),
            new ColumnData("Email", 100, SwingConstants.LEFT, 5),
            new ColumnData("Số điện thoại", 100, SwingConstants.LEFT, 6),
            new ColumnData("Tình trạng", 100, SwingConstants.LEFT, 10),
        };
        DefaultObjectTableModel tableModel = new DefaultObjectTableModel(tblList, tableColumns, customers, false);

        headerTable = tableModel.getHeaderTable();
        headerTable.createDefaultColumnsFromModel();
        tblList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        tblList.getSelectionModel().addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                tblListSelectionValueChanged(evt);
            }
        });

        // Put it in a viewport that we can control a bit
        JViewport viewport = new JViewport();
        viewport.setView(headerTable);
        viewport.setPreferredSize(headerTable.getMaximumSize());

        scpList.setRowHeader(viewport);
        scpList.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, headerTable
            .getTableHeader());
        tblList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblListMouseClicked(evt);
            }
        });
        scpList.setViewportView(tblList);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(scpList, gridBagConstraints);

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/add.png"))); // NOI18N
        btnAdd.setText("Thêm");
        btnAdd.setToolTipText("Thêm mới nhân viên");
        btnAdd.setMaximumSize(new java.awt.Dimension(80, 26));
        btnAdd.setMinimumSize(new java.awt.Dimension(80, 26));
        btnAdd.setPreferredSize(new java.awt.Dimension(80, 26));
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        add(btnAdd, gridBagConstraints);

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/edit.png"))); // NOI18N
        btnEdit.setText("Sửa");
        btnEdit.setToolTipText("Hiệu chỉnh thông tin nhân viên");
        btnEdit.setMaximumSize(new java.awt.Dimension(80, 26));
        btnEdit.setMinimumSize(new java.awt.Dimension(80, 26));
        btnEdit.setPreferredSize(new java.awt.Dimension(80, 26));
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        add(btnEdit, gridBagConstraints);

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/remove.png"))); // NOI18N
        btnDelete.setText("Xóa");
        btnDelete.setToolTipText("Xóa nhân viên");
        btnDelete.setMaximumSize(new java.awt.Dimension(80, 26));
        btnDelete.setMinimumSize(new java.awt.Dimension(80, 26));
        btnDelete.setPreferredSize(new java.awt.Dimension(80, 26));
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        add(btnDelete, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void tblListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblListMouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() == 2) {
            if (this.tblList.getSelectedRow() != -1) {
                this.addEditPanel.setEditableMode(true);
            } else {
                OptionPane.show(JOptionPane.getFrameForComponent(CustomerPnl.this),
                        "Chưa chọn thông tin cần sửa đổi!", "Thông báo");
            }
        }
    }//GEN-LAST:event_tblListMouseClicked

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        if (this.tblList.getSelectedRow() != -1) {
            this.addEditPanel.setEditableMode(true);
        } else {
            OptionPane.show(JOptionPane.getFrameForComponent(CustomerPnl.this),
                    "Chưa chọn thông tin cần sửa đổi!", "Thông báo");
        }
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        Customer customer = new Customer();
        this.addEditPanel.assignDrink(customer);
        this.addEditPanel.setEditableMode(true);
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if (this.tblList.getSelectedRow() != -1) {
            CursorManager.setWaitCursor(this);

            if (OptionPane.show(JOptionPane.getFrameForComponent(CustomerPnl.this),
                    "Khách hàng và tất cả các thông tin liên quan sẽ bị xoá!\n"
                    + "Có chắc chắn thực hiện thao tác này không?",
                    "Thông báo",
                    OptionPane.MsgTYPE_YESNO) == OptionPane.MsgRESULT_YES) {

                Customer customer = this.customers.get(this.tblList.getSelectedRow());

                if (!Biz.getInstance().deleteCustomer(customer.getId())) {
                    OptionPane.show(JOptionPane.getFrameForComponent(CustomerPnl.this),
                            Biz.getInstance().getLastError(),
                            "Thông báo");
                } else {
                    int rowSelection = this.customers.indexOf(customer);
                    if (rowSelection == this.customers.size() - 1) {
                        this.customers.remove(customer);
                        if (rowSelection != 0) {
                            this.tblList.getSelectionModel().setSelectionInterval(rowSelection - 1,
                                    rowSelection - 1);
                        } else {
                            this.tblList.getSelectionModel().clearSelection();
                        }
                    } else {
                        this.customers.remove(customer);
                    }

                    this.headerTable.updateUI();
                    this.tblList.updateUI();
                }

                if (this.tblList.getSelectedRow() >= 0) {
                    Customer currentCustomer = this.customers.get(this.tblList.getSelectedRow());
                    this.addEditPanel.assignDrink(currentCustomer);
                } else {
                    this.addEditPanel.assignDrink(null);
                }
            }

            CursorManager.setDefaultCursor(this);
        } else {
            OptionPane.show(JOptionPane.getFrameForComponent(CustomerPnl.this),
                    "Chưa chọn thông tin cần xoá!",
                    "Thông báo");
        }
    }//GEN-LAST:event_btnDeleteActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JScrollPane scpList;
    private javax.swing.JTable tblList;
    // End of variables declaration//GEN-END:variables
}
